package cn.com.pc.content.domain.tag;

import cn.com.pc.content.domain.Biz;
import cn.com.pc.content.domain.Site;
import cn.com.pc.content.jpa.BizConverter;
import cn.com.pc.content.jpa.SiteConverter;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Data;
import lombok.ToString;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 内容标签，标签为一个简单的描述，使用 "领域:标签值" 形式
 */
@Entity
@DynamicUpdate
@DynamicInsert
@Table(name = "tags")
@Data
public class Tag {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "tb_generator")
    @GenericGenerator(name = "tb_generator", strategy = "cn.com.pc.content.jpa.TagManulGenerator")
    @Column(name = "id")
    private Long id;

    /**
     * 站点
     */
    @Column(name = "site")
    @Convert(converter = SiteConverter.class)
    private Site site;

    /**
     * 业务
     */
    @Column(name = "biz")
    @Convert(converter = BizConverter.class)
    private Biz biz;

    /**
     * 标签名
     */
    @Column(name = "name")
    private String name;

    /**
     * 上架
     */
    @Column(name = "on_shelve")
    private Boolean onShelve;

    /**
     * 创建时间
     */
    @JsonFormat(timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @Column(name = "created_at")
    private LocalDateTime createdAt;

    /**
     * 修改时间
     */
    @JsonFormat(timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    /**
     * 标签树id
     */
    @Column(name = "tree_id")
    private Long treeId;

    /**
     * 关联标签
     */
    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "relationKey.left")
//    @JoinColumn(name = "left_id")
    private List<TagRelation> leftRelations;

    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "relationKey.right")
//    @JoinColumn(name = "right_id")
    private List<TagRelation> rightRelations;

    public String toString() {
        return "id:" + id + ",site:" + site + ",biz:" + biz + ",name:" + name + ",onShelve:" + onShelve + ",createdAt:" + createdAt + ",updatedAt:" + updatedAt + ",treeId:" + treeId;
    }
}
